//
//  OTAModel.swift
//  爱汽车
//
//  Created by tucici on 2024/1/20.
//

import UIKit
import SwiftyJSON

class OTAModel: NSObject {
    @objc dynamic var forceUpdate = false
    @objc dynamic var ota = ""
    @objc dynamic var fileName = ""
    @objc dynamic var versionCode = 0
    @objc dynamic var fileSize = 0
    @objc dynamic var platform = ""
    @objc dynamic var identificationCode = ""
    @objc dynamic var fileType = ""
    @objc dynamic var content = ""
    @objc dynamic var broadcastId = ""
    
    @objc dynamic var localPath = ""
    @objc dynamic var data = Data()
    
    //MARK: -根据fileName拆分获得的属性
    
    /// MCU 编号
    @objc dynamic var tag = ""
    /// BIN 文件类型识别码(文件用途)
    @objc dynamic var code = ""
    /// BIN 文件的匹配码
    @objc dynamic var binCode = ""
    /// BIN 文件的版本号
    @objc dynamic var binVersion = ""

    class func json(_ jsonData:JSON)->OTAModel{
        let s = OTAModel()
        s.forceUpdate  = jsonData["forceUpdate"].boolValue
        s.ota   = jsonData["ota"].stringValue
        s.fileName  = jsonData["fileName"].stringValue
        s.versionCode   = jsonData["versionCode"].intValue
        s.fileSize  = jsonData["fileSize"].intValue
        s.platform   = jsonData["platform"].stringValue
        s.identificationCode  = jsonData["identificationCode"].stringValue
        s.fileType   = jsonData["fileType"].stringValue
        s.content  = jsonData["content"].stringValue
        s.broadcastId  = jsonData["broadcastId"].stringValue
        
        ///文件命名规则截取，获取参数
        let array = s.fileName.components(separatedBy: "_")
        
        ///获取文件用途
        let str = array[6]
        s.code = str[2...str.count-1]
        
        ///文件命名规则截取，获取BIN文件版本号
        var binVersion = array[4]
        binVersion = binVersion[1...binVersion.count-1]
        binVersion = binVersion.fixString(8, "0")
        s.binVersion = binVersion
        
        ///文件命名规则截取， BIN文件匹配码
        var binCode = array[5]
        binCode = binCode[2...binCode.count-1]
        s.binCode = binCode
        return s
    }
}
